﻿/* 
 * yaku
 * http://code.google.com/p/yaku/
 * 
 * Copyright 2011, Bryan McKelvey
 * Licensed under the MIT license
 * http://www.opensource.org/licenses/mit-license.php
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Threading;
using System.Xml;

namespace yaku
{
    /// <summary>
    /// Some extension methods.
    /// </summary>
    public static class ExtensionMethods
    {
        private static Action emptyDelegate = delegate() { };

        /// <summary>
        /// Forces a repaint of the specified UI element.
        /// </summary>
        /// <param name="uiElement">A UI element.</param>
        public static void Refresh(this UIElement uiElement)
        {
            uiElement.Dispatcher.Invoke(DispatcherPriority.Render, emptyDelegate);
        }

        /// <summary>
        /// Removes the last character from this instance.
        /// </summary>
        /// <param name="builder">The StringBuilder object.</param>
        /// <returns>The StringBuilder object.</returns>
        public static StringBuilder DeleteLastChar(this StringBuilder builder)
        {
            return builder.Remove(builder.Length - 1, 1);
        }

        /// <summary>
        /// Gets the last character from a string.
        /// </summary>
        /// <param name="str">A string.</param>
        /// <returns>The last character from a string.</returns>
        public static char GetLastChar(this string str)
        {
            int length = str.Length;
            return (length > 0 ? str[length - 1] : ' ');
        }

        /// <summary>
        /// Gets all but the last character from a string.
        /// </summary>
        /// <param name="str">A string.</param>
        /// <returns>A string containing all characters but the last from a string.</returns>
        public static string GetAllButLastChar(this string str)
        {
            int length = str.Length;
            return (length > 0 ? str.Substring(0, length - 1) : "");
        }
    }
}
